package whisper.service.coder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import whisper.common.utils.FileUtils;
import whisper.service.coder.ffmpeg.FfmpegCommand;
import whisper.service.coder.qtfaststart.QtFaststartCommand;
import whisper.service.constants.MediaInfo;
import whisper.service.constants.UtilsInfo;
import whisper.service.domain.db.Task;


/**
 * Created by JT on 2018/5/7
 */
@Component
public class Transcoder {

    @Autowired
    private MediaInfo mediaInfo;

    @Autowired
    private UtilsInfo utilsInfo;

    public void run(Task task) {

        String sourcePath = mediaInfo.getMediaParentUploadPath() + task.getSourcePath();
        String tempPath = mediaInfo.getMediaTempParentPath() + task.getTargetPath();
        String targetPath = mediaInfo.getMediaParentTranscoePath() + task.getTargetPath();
        String ffmpegPath = utilsInfo.getUtilTranscodeFfmpegPath();
        String qtFaststartPath = utilsInfo.getUtilTranscodeQtFaststartPath();


        try {
            FileUtils.createParentDirs(tempPath);
            FileUtils.createParentDirs(targetPath);
        } catch (Exception e) {
            throw new RuntimeException("Create Directory Failed!");
        }

        CommandExec.run(new FfmpegCommand(ffmpegPath, sourcePath, tempPath).coverIfExists().threads(2));
        CommandExec.run(new QtFaststartCommand(qtFaststartPath, tempPath, targetPath));

    }
}
